package org.thirdHero.db.rabbitmq;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



/**
 * @title rabbitmq配置类
 * @author zengzp
 * @time 2018年8月20日 上午10:46:43
 * @Description 
 */
@Configuration
public class RabbitConfig {
    
    // 支付超时延时交换机
    public static final String Delay_Exchange_Name = "delay.exchange";

    //商家超时未接单交换机
    public static final String Delay_Exchange_Receive_Name = "receive.exchange";
    //商家超时未发货
    public static final String Shop_Shipped_Over_Exchange_Time = "shopOverTime.exchange";
    //商家结算
    public static final String Shipped_Settle_Exchange_Name="settle.exchange";
    //发起售后的自动分账
    public static final String AFTER_SALER_EXCHAGNE_NAME = "after.exchange";
    // 超时订单关闭队列
    public static final String Timeout_Trade_Queue_Name = "close_trade";
    //商家超时未接单退款队列
    public static final String Timeout_Refund_Queue_Name ="refund_trade";
    //商家超时未发货
    public static final String Shop_Shipped_Over_Queue_Name = "shopShippedOver_trade";
    //商家结算
    public static final String Settle_Queue_Name = "settle_trade";
    //发起售后的自动分账
    public static final String AFTER_SALER_QUEUE_NAME = "after_trade";



    @Bean
    public Queue delayPayQueue() {
        return new Queue(RabbitConfig.Timeout_Trade_Queue_Name, true);
    }

    @Bean Queue refundPayQueue(){
        return new Queue(RabbitConfig.Timeout_Refund_Queue_Name,true);
    }

    @Bean Queue shopShippedOverQueue(){ return new Queue(RabbitConfig.Shop_Shipped_Over_Queue_Name,true);}

    @Bean Queue settleQueue(){ return new Queue(RabbitConfig.Settle_Queue_Name,true);}

    @Bean Queue afterSaleQueue(){ return new Queue(RabbitConfig.AFTER_SALER_QUEUE_NAME,true);}

    // 定义广播模式的延时交换机 无需绑定路由
    @Bean
    FanoutExchange delayExchange(){
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        FanoutExchange topicExchange = new FanoutExchange(RabbitConfig.Delay_Exchange_Name, true, false, args);
        topicExchange.setDelayed(true);
        return topicExchange;
    }
    @Bean
    FanoutExchange refundExchange(){
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        FanoutExchange topicExchange = new FanoutExchange(RabbitConfig.Delay_Exchange_Receive_Name,true,false,args);
        topicExchange.setDelayed(true);
        return topicExchange;
    }

    @Bean
    FanoutExchange shopShippedOverExchange(){
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        FanoutExchange fanoutExchange = new FanoutExchange(RabbitConfig.Shop_Shipped_Over_Exchange_Time,true,false,args);
        fanoutExchange.setDelayed(true);
        return fanoutExchange;
    }
    @Bean
    FanoutExchange AfterSaleExchange(){
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        FanoutExchange fanoutExchange = new FanoutExchange(RabbitConfig.Shipped_Settle_Exchange_Name,true,false,args);
        fanoutExchange.setDelayed(true);
        return fanoutExchange;
    }
    @Bean
    FanoutExchange ShippedSettleExchange(){
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        FanoutExchange fanoutExchange = new FanoutExchange(RabbitConfig.AFTER_SALER_EXCHAGNE_NAME,true,false,args);
        fanoutExchange.setDelayed(true);
        return fanoutExchange;
    }
    // 绑定延时队列与交换机
    @Bean  
    public Binding delayPayBind() {  
        return BindingBuilder.bind(delayPayQueue()).to(delayExchange());  
    }

    @Bean
    public Binding delayRefundBind(){
        return BindingBuilder.bind(refundPayQueue()).to(refundExchange());
    }
    @Bean
    public Binding shippedSettleBind(){return BindingBuilder.bind(settleQueue()).to(ShippedSettleExchange()); }
    @Bean
    public Binding ShopShippedOverBind(){return BindingBuilder.bind(shopShippedOverQueue()).to(shopShippedOverExchange());}
    @Bean
    public Binding afterSaleBind(){return BindingBuilder.bind(afterSaleQueue()).to(AfterSaleExchange());}

    // 定义消息转换器
    @Bean
    Jackson2JsonMessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    
    // 定义消息模板用于发布消息，并且设置其消息转换器
    @Bean
    RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate;
    }
    @Bean
    RabbitAdmin rabbitAdmin(final ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

}